<template>
  <div>
    <h1>计算总和:{{ sum }}</h1>
    <select v-model="n">
      <option :value="1">1</option>
      <option :value="2">2</option>
      <option :value="3">3</option>
    </select>
    <button @click="add">+</button>
    <button @click="sub">-</button>
    <button @click="addOdd">总和为奇数时+1</button>
    <button @click="addWait">过一会在加</button>
  </div>
</template>

<script>
export default {
  name: "my-calculate",
  data() {
    return {
      sum: 0,
      n: 1
    }
  },
  methods: {
    add() {
      this.sum += this.n;
    },
    sub() {
      this.sum -= this.n;
    },
    addOdd() {
      if (this.sum % 2) {
        this.sum += this.n;
      }
    },
    addWait() {
      setTimeout(() => {
        this.sum += this.n;
      }, 500)
    }
  }
}
</script>

<style scoped>
  button {
    margin-right: 5px;
  }
</style>